package com.dmagic.cms.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dmagic.cms.common.dao.CommonDao;
import com.dmagic.cms.common.exception.ServiceException;
import com.dmagic.cms.common.exception.ValidateException;
import com.dmagic.cms.common.query.PageUtil;
import com.dmagic.cms.common.service.CommonService;
import com.dmagic.cms.common.util.StringUtil;
import com.dmagic.cms.dao.CompanyDao;
import com.dmagic.cms.entity.Company;
import com.dmagic.cms.service.CompanyService;

@Service
@Transactional(readOnly = true, rollbackFor = ValidateException.class)
public class CompanyServiceImpl extends CommonService<Company> implements CompanyService {
	@Autowired
	private CompanyDao companyDao;

	@Override
	protected CommonDao<Company, Serializable> getCommonDao() {
		return (CommonDao) companyDao;
	}

	@Override
	public List<Company> getCompanyPropertyName(String propertyName,
			Object value) throws ServiceException {
		return companyDao.findBy(propertyName, value);
	}

	@Override
	@Transactional(readOnly = false)
	public void saveCompany(Company[] company) throws ValidateException {
		for (Company n : company) {
			if (StringUtil.isBlank(n.getCompanyName())) {
				continue;
			}
			saveEntity(n);
		}
	}

	@Override
	public void searchContactUs(PageUtil<Map> pageUtil) throws ServiceException {
		try {
			companyDao.searchContactUs(pageUtil);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	@Override
	public void searchAboutUs(PageUtil<Map> pageUtil) throws ServiceException {
		try {
			companyDao.searchAboutUs(pageUtil);
		} catch (Exception e) {
			throw new ServiceException(e);
		}
	}

	@Override
	public Company getCompany(Long companyId) throws ServiceException {
		return getEntityById(companyId);
	}

	public List<Company> getCompanyByCompanyNo(String companyNo) {
		return companyDao.findBy("companyNo", companyNo);
	}

	@Override
	@Transactional(readOnly = false)
	public void deleteCompany(List<Long> companyIds) throws ServiceException {
		for (Long companyId : companyIds) {
			deleteEntityById(companyId);
		}
	}

	@Override
	@Transactional(readOnly = false)
	public void deleteCompany(Long companyId) throws ServiceException {
		deleteEntityById(companyId);
	}
}
